Musiker.modNowPlaying = {
	name			: 	"NowPlaying"

}

Musiker.modNowPlaying.Initialize = function () {
	var aFrame = document.createElement('div');
		aFrame.className = 'cssNowPlayingFrame';
		aFrame.show = MUSIKER_SHOWPANEL;
		aFrame.hide = MUSIKER_HIDEPANEL;		
		Musiker.modNowPlaying.Panel = aFrame
		aFrame.hide();
	Musiker.frames.DisplayPanel.appendChild(aFrame);
	
		//wrapping div
		var cFrame = document.createElement('div');
			cFrame.className = 'scrollingWrapper';
			cFrame.style.top = '17px';
			cFrame.style.bottom = '33px';
		Musiker.modNowPlaying.Panel.appendChild(cFrame);	
	
		//contentTable
		var aFrame = document.createElement('table');
			aFrame.className = 'cssPlaylist';
			Musiker.modNowPlaying.listPanel = aFrame;
		cFrame.appendChild(aFrame);

		//header
		var aFrame = document.createElement('table');
			aFrame.className = 'cssPlaylistHeader';
			aFrame.innerHTML = "<tr class='cssPlaylistItem' header='yes'><td class='cssPlaylistItem_Num'>#</td><td class='cssPlaylistItem_Title'>Title</td><td class='cssPlaylistItem_Artist'>Artist</td><td class='cssPlaylistItem_Album'>Album</td><td></td></tr>";
		Musiker.modNowPlaying.Panel.appendChild(aFrame);


		//action bar
		var aFrame = document.createElement('div');
			aFrame.className = 'cssActionBar';
			Musiker.modNowPlaying.actionBarPanel = aFrame;
		Musiker.modNowPlaying.Panel.appendChild(aFrame);
		
			var aFrame = document.createElement('a');
				aFrame.className = 'cssActionBarButton';
				aFrame.innerHTML = 'Remove All';
				aFrame.onclick = function () {Musiker.modNowPlaying.purgeAll();}
				Musiker.modNowPlaying.actionBarPanel.buttonRemoveAll = aFrame;
			Musiker.modNowPlaying.actionBarPanel.appendChild(aFrame);

			var aFrame = document.createElement('a');
				aFrame.className = 'cssActionBarButton';
				aFrame.innerHTML = 'Build M3U File';
				aFrame.setAttribute('enabled',true);
				aFrame.onclick = function () {
					Musiker.modNowPlaying.buildM3U();
				}
				Musiker.modNowPlaying.actionBarPanel.buttonBuildM3U = aFrame;
			Musiker.modNowPlaying.actionBarPanel.appendChild(aFrame);


	//	Setup Sidebar Row --------------------------------------------------------------------------------------
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebar_Row';
		aFrame.innerHTML = "<img src='images/iTunesPartyShuffle.png'><a>Now Playing</a>";
		aFrame.onclick = function () {
			Musiker.selectModule(Musiker.modNowPlaying);
		}
		Musiker.modNowPlaying.sidebarItem = aFrame;
	Musiker.frames.SidebarPanel.appendChild(aFrame);

}

Musiker.modNowPlaying.Start = function () {
	
}

Musiker.modNowPlaying.show = function () {
	Musiker.modNowPlaying.rebuildList();
	Musiker.modNowPlaying.Panel.show();
	Musiker.Sidebar.setSelection(Musiker.modNowPlaying.sidebarItem);
}
Musiker.modNowPlaying.hide = function () {
	Musiker.modNowPlaying.Panel.hide();
}

Musiker.modNowPlaying.rebuildList = function () {
	while (Musiker.modNowPlaying.listPanel.childNodes.length > 0) {
		Musiker.modNowPlaying.listPanel.removeChild( Musiker.modNowPlaying.listPanel.firstChild );       		  
	}
	Musiker.modNowPlaying.listPanel.currentSelect = null;
	Musiker.modNowPlaying.listPanel.itemArray = {};
		
	//row items
	var oddfield = 0;
	var c = MediaPlayer.playlistLength();
	var i = MediaPlayer.defaultState() ? 1 : 0;
	for (;i<c;i++) {
		var o = MediaPlayer.playlistItem(i);
			o.artist = o.author;
			if (o.album=='null') o.album = null;
		oddfield = ++oddfield % 2;
		
		var aFrame = Musiker.buildRow(o,i+1,oddfield);
			aFrame.playIndex = i;
			aFrame.ondblclick = function () {
					MediaPlayer.playTrack(this.playIndex);
					return false;
				}
			if (i == MediaPlayer.currentItemIndex) aFrame.firstChild.setAttribute('currentSong','true');
			var cFrame = document.createElement('img');
				cFrame.src = 'images/closeWidget.png';
				cFrame.playIndex = i;
				cFrame.onclick = function () {
					if (MediaPlayer.currentItemIndex == this.playIndex) Musiker.modPlayer.clearStatus();
					MediaPlayer.removeTrack(this.playIndex);
					Musiker.modNowPlaying.rebuildList();
				}
			aFrame.lastChild.appendChild(cFrame);
			
		Musiker.modNowPlaying.listPanel.appendChild(aFrame);
		Musiker.modNowPlaying.listPanel.itemArray[i] = aFrame;
	}
	
	if (c>1) Musiker.modNowPlaying.actionBarPanel.buttonRemoveAll.setAttribute('enabled',true);
	else Musiker.modNowPlaying.actionBarPanel.buttonRemoveAll.removeAttribute('enabled');
	
}

Musiker.modNowPlaying.update = function () {
	if (Musiker.currentModule == Musiker.modNowPlaying) {
		for (i in Musiker.modNowPlaying.listPanel.itemArray) {
			aFrame = Musiker.modNowPlaying.listPanel.itemArray[i];
			if (aFrame.playIndex == MediaPlayer.currentItemIndex) aFrame.firstChild.setAttribute('currentSong','true');
			else /*if (aFrame.attributes.getNamedItem('currentSong'))*/ aFrame.firstChild.removeAttribute('currentSong');
		}
	}
}

Musiker.modNowPlaying.buildM3U = function () {
	var songsList = new Array();
	var c = MediaPlayer.playlistLength();
	var i = MediaPlayer.defaultState() ? 1 : 0;
	for (;i<c;i++) {
		var o = MediaPlayer.playlistItem(i);
		
		songsList[i] = o.id;
	}
	console.log(songsList);
	Musiker.requestM3U(songsList);
}

Musiker.modNowPlaying.purgeAll = function () {
	MediaPlayer.removeAllTracks();
	
	if (MediaPlayer.currentItemIndex==0) Musiker.modPlayer.clearStatus();
	
	Musiker.modNowPlaying.rebuildList();
}

Musiker.requestM3U = function (songs) {
	$('m3u_filelist').value = songs.join('!');
	$('m3u_request').submit();
}

Musiker.RegisterModule(Musiker.modNowPlaying);